#!/usr/bin/python
# coding=utf-8
   
"""批量打包的bat

python imaging_bat.py -r 0 -i 2 -x 1 -b path
"""
__author__ = 'x'
__email__ = 'x@gmail.com' 
VERSION = "v1.0"

import os, sys, re, time, getopt, string, datetime
import shutil
import glob
import Image
import ConfigParser

tihuan = []
info = []

opts, args = getopt.getopt(sys.argv[1:], "hb:r:i:x:", ["help", "base=", "transpose=", "dir_type=", "append="])
#基目录*
base_path = ""
#是否旋转
is_transpose = '0'
#1-单个，2-批量
dir_type = "1"
#追加，append等于“1”表示不追加
append = "1"

def usage():
    print ("Usage:imaging_bat.py [ops]")

if not opts:
    usage()
    sys.exit()
else:
    for op, value in opts:
#        print "op:value="+op+":"+value
        if op == "-b" or op == "--base":
            base_path = value
        elif op == "-r" or op == "--transpose":
            is_transpose = value
        elif op == "-i" or op == "--dir_type":
            dir_type = value        
        elif op == "-x" or op == "--append":
            append = value        
        elif op == "-h" or op == "--help":
            usage()            
            sys.exit()

            

def main():
    '''主函数'''
    ngapktool_path = "F:\\wwplan\\hack\\tool\\NgApkTool"
    screenshots_dir_name = "Screenshots"
    screenshots_dir_name_ok = "_OK"
    
    if not os.path.exists(base_path):
        print "root path not exists"
        sys.exit()

    dir_list = []
    dir_name_list = []
    bad_item_list = []
    bad_item_name_list = []
    #判断是否是批量
    if dir_type == "2":
        #扫描目录
        for i in os.listdir(base_path):
            item_path = base_path + "\\" + i;
            if not os.path.isdir(item_path):
                bad_item_list.append(item_path)
                bad_item_name_list.append(i)
                continue
            item_path = item_path+"\\"+screenshots_dir_name
            if not os.path.isdir(item_path):
                bad_item_list.append(item_path)
                bad_item_name_list.append(i)
                continue
            #如果是不追加
            if append == "1" :
                if os.path.isdir(item_path+screenshots_dir_name_ok):
                    shutil.rmtree(item_path+screenshots_dir_name_ok)
            dir_list.append(item_path)
            dir_name_list.append(i)
    elif dir_type == "1":
        dir_list.append(base_path)

    i = 0
    for item_path in dir_list:
        print "do........"
        os.system('python '+ngapktool_path+'\\imaging.py -s %s -t %s -r %s' % (item_path, item_path+screenshots_dir_name_ok, is_transpose))
        print dir_name_list[i]+" screenshots is ok!"
        i = i + 1;
    if bad_item_name_list:
        print "not has screenshots item:"
        for name in bad_item_name_list:
            print name
    
    print "imaging bat is Complete!"

if __name__ == "__main__":
  sys.exit(main())
